package com.mo.controller;

import com.github.pagehelper.PageInfo;
import com.mo.model.TActivity;
import com.mo.query.ActivityQuery;
import com.mo.result.R;
import com.mo.service.ActivityService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author mo
 * version
 */
@RestController
public class ActivityController {
    @Resource
    private ActivityService activityService;
    @GetMapping("/api/activities")
    public R activityPage(@RequestParam(value = "current",required = false) Integer current, ActivityQuery activityQuery){
        if (current==null){
            current=1;
        }
        PageInfo<TActivity> pageInfo = activityService.getUserByPage(current,activityQuery);
        return R.OK(pageInfo);
    }

    @PostMapping("/api/activity")
    public R Activity(ActivityQuery activityQuery) {
        int count =  activityService.saveActivity(activityQuery);
        return count >= 1 ? R.OK() : R.FAIL();
    }
    @GetMapping("/api/activity/{id}")
    public R loadActivity(@PathVariable Integer id){
        TActivity activity=activityService.getActivityById(id);
        return R.OK(activity);
    }
    @PutMapping("/api/activity")
    public R editActivity(ActivityQuery activityQuery) {
        int count =  activityService.updateActivity(activityQuery);
        return count >= 1 ? R.OK() : R.FAIL();
    }
    @DeleteMapping("/api/activity/{id}")
    public R deleteActivity(@PathVariable("id") Integer id){
        int count=activityService.deleteActivityById(id);
        return count >= 1 ? R.OK() : R.FAIL();
    }
    @DeleteMapping("/api/activities")
    public R batchDelUser(@RequestParam(value = "ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = activityService.batchDelActivityByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

}
